/**
 * 
 */
package DBLayer;
import java.sql.*;
import java.util.ArrayList;
import ModelLayer.*;
/**
 * @author 
 * 
 */
public class DBBetaling 
{
	/**
	 * Initializes instance variables
	 */
	private  Connection con;

	/** Creates connection to the database and creates an instance of DBBetaling*/
	public DBBetaling() 
	{
		con = DbConnection1.getInstance().getDBcon();
	}

	/** getAllBetalinger get all Betalinger with
	 * 
	 * @param retriveAssociation (tells weather or not the returned Betalinger needs to be associated with Salg)
	 * @return
	 */
	public ArrayList<Betaling> getAllBetalinger(boolean retriveAssociation)
	{
		return miscWhere("", retriveAssociation);
	}
	
	/** getBetalingId finds BetalingId with
	 * 
	 * @param betalingId
	 * @param retriveAssociation(tells weather or not the returned BetalingID needs to be associated with SalgId)
	 * @return
	 */
	
	public Betaling getBetalingId(int betalingId, boolean retriveAssociation)
	{   
		String wClause = "  betalingId = '" + betalingId + "'";
		return singleWhere(wClause, retriveAssociation);
	}
	
	/** insertBetaling creates a betaling SQL query to be inserted into database with
	 * 
	 * @param bet
	 * @return
	 * @throws Exception (If betaling is not found)
	 */
	public int insertBetaling(Betaling bet) throws Exception
	{  
		//System.out.println("next betalId = " +  nextBetalId);
		int rc = -1;
		//System.out.println("insertBetaling");

		String query="INSERT INTO Betaling(betalingId, betalingsDato, totalPris, betalingsStatus)  VALUES("+
				bet.getBetalId()  + ",'"  +
				bet.getBetalingsDato()  + "',"  +			   
				bet.getTotalPris()  + ",'"  +
				bet.getBetalingsStatus()  + "')";
		//System.out.println("insertBetalingEfter");

		//System.out.println("insert : " + query);
		try
		{ // insert new betaling
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		}//end try
		catch(SQLException ex)
		{
			//System.out.println("Betaling ikke oprettet");
			throw new Exception ("Betaling is not inserted correct");
		}
		return(rc);
	}
	/** updateBetaling creates an update betaling SQL query to update a betaling in the database with
	 * 
	 * @param bet
	 * @return
	 */
	public int updateBetaling(Betaling betaling)
	{
		Betaling bObj  = betaling;
		int rc=-1;

		String query="UPDATE betaling SET "+
				"totalPris ='"+ bObj.getTotalPris()+"', "+
				"betalingsStatus ='"+bObj.getBetalingsStatus() + "' " +                         
				" WHERE betalingId = '"+ bObj.getBetalId() + "' ";
		//System.out.println("Update query:" + query);
		try
		{ // update betaling
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);

			stmt.close();
		}//slut try
		catch(Exception ex)
		{
			//System.out.println("Update exception in betaling db: "+ex);
		}
		return(rc);
	}	

	/**
	 * Singelwhere is used when we only want to select one betaling 	
	 * @param wClause
	 * @param retrieveAssociation(tells weather or not the returned BetalingID needs to be associated with SalgId)
	 * @return
	 */
	private Betaling singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Betaling bObj = new Betaling();

		String query =  buildQuery(wClause);
		// System.out.println(query);
		try
		{ // read the betaling from the database
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			if(results.next())
			{
				bObj = buildBetaling(results);
				//Association is to be build
				stmt.close();
				if(retrieveAssociation)
				{   
					//association to Salg
					DBSalg dbsalg = new DBSalg();
					Salg salg = dbsalg.getSalg(bObj.getBetalId(), false);
					//System.out.println("Salg is selected ");
					bObj.setBetalId(salg.getSalgId());
				}	
			}                 
		}//end try	
		catch(Exception e){
			//System.out.println("Query exception: "+e);
		}
		return bObj;
	}
	
	/** mischWere is used whenever we want to select more than one betaling or all betalinger
	 * @param wClause
	 * @param retrieveAssociation(tells weather or not the returned BetalingID needs to be associated with SalgId)
	 * @return
	 */

	private ArrayList<Betaling> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<Betaling> list = new ArrayList<Betaling>();	

		String query =  buildQuery(wClause);

		try
		{ // read the betaling from the database
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while( results.next() ){
				Betaling bObj = new Betaling();
				bObj = buildBetaling(results);	
				list.add(bObj);
				//System.out.println("\n SalgsId: " + bObj.getBetalId() +  "\n Betalingsdato: " + bObj.getBetalId() +  "	\n Totalpris:  " + bObj.getTotalPris() + "	\n Status " + bObj.getBetalingsStatus());
			}//end while
			stmt.close();     

		}//end try	
		catch(Exception e)
		{
			//System.out.println("Query exception - select: "+e);
			e.printStackTrace();
		}
		return list;
	}

	/** buildQuery uses wClause insert String list of the attributes in a Betaling and builds the
	 * WHERE part of the query that is to be committed to the SQl database
	 * 
	 * @param wClause 
	 * @return
	 */
	private String buildQuery(String wClause)
	{
		String query="SELECT betalingId, betalingsDato, totalPris, betalingsStatus  FROM betaling";

		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;

		return query;
	}
	/**
	 * method to build a Betaling object 
	 * @param results (used to bring attributes to bObj)
	 * @return
	 */
	private Betaling buildBetaling(ResultSet results)
	{   
		Betaling bObj = new Betaling();
		try
		{ // the columns from the table kunde are used
			bObj.setBetalId(results.getInt("betalingId"));
			bObj.setBetalingsDato(results.getString("betalingsDato"));
			bObj.setTotalPris(results.getDouble("totalPris"));
			bObj.setBetalingsStatus(results.getBoolean("betalingsStatus"));
		}
		catch(Exception e)
		{
			//System.out.println("error in building the betaling object");
		}
		return bObj;
	}
}
